<?php declare(strict_types=1);


namespace App\Api\V2\Controller\Bill\Merchant;

use App\Api\Controllers\BaseController;
use App\Models\MerchantStore;
use App\Models\Store;
use App\Services\Count\MerchantBillCountService;
use App\Services\Count\UserBillCountService;
use App\Services\PrintSqlService;
use App\Traits\ResponseTrait;
use Illuminate\Http\Request;

class CountController extends BaseController
{
    use ResponseTrait;


    public function merchantBiCount(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $merchant_id = $user->merchant_id;
            //在这里处理商户的对账统计
            list($storeP, $storeIds) = $this->getMerchantWhere($request, $merchant_id);


            $where = [
                "curHdIndex" => $request->get("curHdIndex"),
                "startDate" => $request->get("startDate"),
                "endDate" => $request->get("endDate"),
                "store_pid" => $storeP->store_id,
                "store_id" => $storeIds,
                "pay_type" => $request->get("pay_type"),
                "company" => $request->get("company"),
                "total_fee" => $request->get("total_amount"),
                "receive_model" => $request->get("receive_model"),
                "cashier_id" => $request->get("merchant_id"),
            ];
            $service = new MerchantBillCountService();
            //在这里做一个新的统计
            $isTime = false;
            if (!empty($where["startDate"]) && !empty($where["endDate"])) {
                $isTimeStart = date("H:i:s", strtotime($where["startDate"]));
                $isTimeEnd = date("H:i:s", strtotime($where["endDate"]));
                if ($isTimeStart == "00:00:00" && $isTimeEnd == "23:59:59") {
                    $isTime = false;
                } else {
                    $isTime = true;
                }
            }
//            PrintSqlService::listen();
            if ($isTime) {
                $data = $service->merchantGetTime($where);
            } else {
                $data = $service->merchantGet($where);
            }
            return $this->successResponse("ok", ["data" => $data]);

        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }
}
